package com.hzp.controller;

import com.hzp.entity.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@RequestMapping("/consumer/dept")
public class ConsumerDeptController {

    // 远程访问地址
    private static final String REST_URL_PREFIX = "http://provider-server";
    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/list")
    public List<Dept> listDept(){
        //restTemplate.getForObject(REST请求的地址(提供者的地址)，HTTP响应转换被转换是的对象)
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/list", List.class);
    }

    @RequestMapping("/get/{id}")
    public Dept getDept(@PathVariable("id") Integer id){
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get/"+id, Dept.class);
    }

    @RequestMapping("/add")
    public boolean addDept(@RequestBody Dept dept){
        //restTemplate.postForObject(REST请求的地址(提供者的地址)，请求的参数，HTTP响应转换被转换是的对象)
        return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add", dept, Boolean.class);
    }
}